package com.xiaoke.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.xiaoke.entity.Goods;
import com.xiaoke.entity.GoodsGallery;
import com.xiaoke.entity.GoodsPrice;
import com.xiaoke.mapper.*;
import com.xiaoke.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class GoodsServiceImpl implements GoodsService {
    @Autowired
    private GoodsMapper goodsMapper;
    @Autowired
    private GoodsGalleryMapper goodsGalleryMapper;
    @Autowired
    private CommentMapper commentMapper;
    @Autowired
    private GoodsPriceMapper goodsPriceMapper;
    @Autowired
    private CollectMapper collectMapper;
    @Autowired
    private CommentPictureMapper commentPictureMapper;

    @Override
    public Goods selectByPrimaryKey(String id) {
        return goodsMapper.selectByPrimaryKey(id);
    }

    public List<Goods> findByParams(@RequestBody Map<String, Object> params, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize, true, false, false);
//        PageHelper.orderBy("add_time desc");
        Page<Goods> page = goodsMapper.findByParams(params);

        return page.getResult();
    }

    @Override
    public Map<String, Object> getDetailById(String id) {
        Goods info = goodsMapper.selectByPrimaryKey(id);
        List<GoodsGallery> gallery = goodsGalleryMapper.findByGoods(id);
        List<GoodsPrice> goodsPriceList = goodsPriceMapper.findByGoods(id);
        Integer commentNum = commentMapper.countByGoods(id);
        List<Map<String, String>> comments = commentMapper.findTop10ByGoods(id);
        for (Map c: comments) {
            String cid = (String) c.get("id");
            List<String> pic_list = commentPictureMapper.findByComment(cid);
            c.put("pic_list",pic_list);
        }

        Map comment = new HashMap();
        comment.put("count", commentNum);
        comment.put("data", comments);

        Map res = new HashMap();
        res.put("info", info);
        res.put("gallery", gallery);
        res.put("goodsPriceList", goodsPriceList);
        res.put("comment", comment);

        return res;
    }

    @Override
    public List<Map<String, Object>> getByParams(@RequestBody Map<String, Object> params) {
        PageHelper.startPage(Integer.parseInt(params.get("pageSize").toString()), Integer.parseInt(params.get("pageNo").toString()), true, false);
        return goodsMapper.getByParams(params);
    }

    @Override
    public Integer countByShop(String shopId) {
        return goodsMapper.countByShop(shopId);
    }

    @Override
    public Map<String, Object> findTotalPriceByIds(String... id) {
        return null;
    }

    @Override
    public Map<String, Object> countTotalPriceByGoodsPriceIds(String... id) {
        return goodsMapper.countTotalPriceByGoodsPriceIds(id);
    }

    @Override
    public List<Map<String, Object>> findByCategoryAndShop(Integer categoryId, String shopId) {
        return goodsMapper.findByCategoryAndShop(categoryId, shopId);
    }

}
